package com.wiloon.java8x.basic.classloader;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * Created by roy.wang on 2017/4/19;
 */
public class ClassLoaderX {
    public static void main(String[] args) throws Exception {

        // printClassLoader();
        // UrlClassLoaderX();
        UrlClassLoaderX0();

        //systemClassLoader();
    }

    private static void systemClassLoader() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        System.out.println();
        System.out.println("System class loader");
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        String reference = "com.wiloon.java8x.basic.classloader.JavaBasicFoo";
        System.out.println("loading class:" + reference);
        Class<?> class1 = classLoader.loadClass(reference);
        Object o = class1.newInstance();
        System.out.println("Object:" + o);
        System.out.println("object get name:" + o.getClass().getName());
        System.out.println("Test的定义类装载器: " + ClassLoaderX.class.getClassLoader());
        System.out.println("Test中直接使用JavaBasicFoo使用的ClassLoader: " + JavaBasicFoo.class.getClassLoader());
        System.out.println("自定义装载器装载 JavaBasicFoo 的定义类加载器: " + o.getClass().getClassLoader());

        JavaBasicFoo castO = (JavaBasicFoo) o;
        System.out.println("System class loader cast object:" + castO);
    }

    private static void UrlClassLoaderX() throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        try {
            System.out.println("url class loader");
            //D:\workspace\myProjects\java8x\java8x\java-basic\build\classes\main\com\wiloon\java8x\basic\classloader
            String url = "file://" + System.getProperty("user.dir").replaceAll("\\\\", "/")
                    + "/java-basic/build/classes/main/";
            System.out.println("url:" + url);
            ClassLoader c1 = new URLClassLoader(new URL[]{new URL(url)}, null);
            System.out.println("c1的父类加载器: " + c1.getParent());
            System.out.println("SystemClassLoader: " + ClassLoader.getSystemClassLoader());
            String reference = "com.wiloon.java8x.basic.classloader.JavaBasicFoo";
            System.out.println("loading class:" + reference);
            Class<?> class1 = c1.loadClass(reference);
            Object o = class1.newInstance();
            System.out.println("Object:" + o);
            System.out.println("object get name:" + o.getClass().getName());
            System.out.println("Test的定义类装载器: " + ClassLoaderX.class.getClassLoader());
            System.out.println("Test中直接使用JavaBasicFoo使用的ClassLoader: " + JavaBasicFoo.class.getClassLoader());
            System.out.println("自定义装载器装载 JavaBasicFoo 的定义类加载器: " + o.getClass().getClassLoader());

            JavaBasicFoo castO = (JavaBasicFoo) o;
            System.out.println("UrlClassLoaderX cast object:" + castO);
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void printClassLoader() {
        System.out.println("print class loader");
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        System.out.println("context class loader:" + classLoader);
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        System.out.println("system class loader:" + systemClassLoader);

        ClassLoader extClassLoader = systemClassLoader.getParent();
        System.out.println("system class loader > parent: ext class loader:" + extClassLoader);
        ClassLoader bootClassLoader = extClassLoader.getParent();
        System.out.println("ext class loader > parent:" + bootClassLoader);
        System.out.println();
    }

    private static void UrlClassLoaderX0() throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        try {
            System.out.println("url class loader 0");

            //System.out.println("Test中直接使用JavaBasicFoo使用的ClassLoader: " + JavaBasicFoo.class.getClassLoader());

            //D:\workspace\myProjects\java8x\java8x\java-basic\build\classes\main\com\wiloon\java8x\basic\classloader
            String url = "file://" + System.getProperty("user.dir").replaceAll("\\\\", "/")
                    + "/java-basic/build/classes/main/";
            System.out.println("url:" + url);
            ClassLoader c1 = new URLClassLoader(new URL[]{new URL(url)}, ClassLoader.getSystemClassLoader());
            System.out.println("c1的父类加载器: " + c1.getParent());
            System.out.println("SystemClassLoader: " + ClassLoader.getSystemClassLoader());
            String reference = "com.wiloon.java8x.basic.classloader.JavaBasicFoo";
            System.out.println("loading class:" + reference);
            Class<?> class1 = c1.loadClass(reference);
            Object o = class1.newInstance();
            System.out.println("Object:" + o);
            System.out.println("object get name:" + o.getClass().getName());
            System.out.println("Test的定义类装载器: " + ClassLoaderX.class.getClassLoader());
            System.out.println("Test中直接使用JavaBasicFoo使用的ClassLoader: " + JavaBasicFoo.class.getClassLoader());
            System.out.println("自定义装载器装载 JavaBasicFoo 的定义类加载器: " + o.getClass().getClassLoader());

            JavaBasicFoo castO = (JavaBasicFoo) o;
            System.out.println("UrlClassLoaderX0 cast object:" + castO);
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
